<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Users;
use app\index\model\Personals;
class Label extends Common
{
	//标签列表
	public function labelList(){
      $uid = Request::instance()->post('uid','');//用户id
		  $labeid = Request::instance()->post('labeid','');//用户标签类型 1个性标签 2喜欢的运动  3喜欢的食物 4我的足迹
        if(empty($uid) || empty($labeid)){
          return json(['state'=>'202','msg'=>'参数不足']);
        }
       $rs=Users::selOne($uid);
       if($rs){
          $result=Personals::labelSel($uid,$labeid);
          if($result){
            return json(['state'=>'200','msg'=>'查询成功','result'=>$result]);
          }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);
          }
       }else{
        return json(['state'=>'201','msg'=>'用户已不存在']);
       }
	}
  //选择标签
  public function labelSet(){
     $uid = Request::instance()->post('uid','');//用户id
      $labeid = Request::instance()->post('labeid','');//1标签类型 2运动  3食物  4足迹
      $keyId = Request::instance()->post('keyId','');//多个id
      if(empty($uid) || empty($labeid)){
          return json(['state'=>'202','msg'=>'参数不足']);
        }
       $rs=Users::selOne($uid);
       if($rs){
          $result=Personals::labelSet($uid,$labeid,$keyId);
          if($result){
            return json(['state'=>'200','msg'=>'修改成功']);
          }else{
            return json(['state'=>'201','msg'=>'修改失败再试试']);
          }
       }else{
        return json(['state'=>'201','msg'=>'用户已不存在']);
       }
  }
  //创建标签
  public function labeladd(){
    $uid=input('uid');//用户id
    $labeid=input('labeid');//标签类型 1个性标签 2喜欢的运动  3喜欢的食物 4我的足迹
    $keyName=input('keyName');//标签名称
    if(empty($uid) || empty($labeid) || empty($keyName)){
      return json(['state'=>'202','msg'=>'参数不足']);
    }
    $re=Db::name('keyword')->where(['uid'=>$uid,'key_type'=>$labeid,'keyName'=>$keyName])->find();
    if(empty($re)){
      $re=Db::name('keyword')->where(['key_type'=>$labeid,'keyName'=>$keyName])->find();
      if(empty($re)){
        $data=['uid'=>$uid,'key_type'=>$labeid,'keyName'=>$keyName];
        $re=Db::name('keyword')->insert($data);
      }
    }
    if($re){
       $result=Db::name('keyword')
       ->where(['uid'=>$uid,'key_type'=>$labeid,'keyName'=>$keyName])
       ->field('id,keyName')
       ->find();
      return json(['state'=>'200','msg'=>'添加成功','result'=>$result]);
    }else{
      return json(['state'=>'201','msg'=>'添加失败再试试']);
    }
  }
  //删除我的标签
  public function labeidDel(){
     $uid=input('uid');//用户id
    $labeid=input('labeid');//标签类型 1个性标签 2喜欢的运动  3喜欢的食物 4我的足迹
    $keyId=input('keyId');//标签名称
    if(empty($uid) || empty($labeid) || empty($keyId)){
      return json(['state'=>'202','msg'=>'参数不足']);
    }
    $re=Db::name('keyword')->where(['uid'=>$uid,'key_type'=>$labeid,'id'=>$keyId])->delete();
    if($re){
      return json(['state'=>'200','msg'=>'删除成功']);
    }else{
      return json(['state'=>'200','msg'=>'删除成功']);
    }
  }
  //我的标签接口
  public function mylabel(){
     $nearby_id=input('nearby_id');//用户id
    if(empty($nearby_id)){
      return json(['state'=>'201','msg'=>'参数不足']);
    }
    $re=Db::name('user_info')->where(['uid'=>$nearby_id,'member_type'=>'1'])->field('key_word,exerciseId,fooId,footprintId')->find();
   // print_r($re);die;
    if(!empty($re['key_word'])){
      $re['key_word']=Db::name('keyword')->where('id','in',$re['key_word'])->field('keyName')->select();
      // array_push($re,"1");
    }else{
      $re['key_word']=[];
    }
    if(!empty($re['exerciseId'])){
       // array_push($re,"2");
       $re['exerciseId']=Db::name('keyword')->where('id','in',$re['exerciseId'])->field('keyName')->select();
    }else{
      $re['exerciseId']=[];
    }
     if(!empty($re['fooId'])){
      
       $re['fooId']=Db::name('keyword')->where('id','in',$re['fooId'])->field('keyName')->select();
    }else{
      $re['fooId']=[];
    }

    if(!empty($re['footprintId'])){
        $re['footprintId']=Db::name('keyword')->where('id','in',$re['footprintId'])->field('keyName')->select();
    }else{
      $re['footprintId']=[];
    }
            
    if($re){
       return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$re]));
     }else{
       return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$re]));
     }
    // if(!empty($re['key_word'])){
    //   $keyword=Db::name('keyword')->where('id','in',$re['key_word'])->field('keyName')->select();
    // }else{
    //   $keyword=[];
    // }
    // if(!empty($re['exerciseId'])){
    //   $exerciseId=Db::name('keyword')->where('id','in',$re['exerciseId'])->field('keyName')->select();
    // }else{
    //    $exerciseId=[];
    // }
    // if(!empty($re['fooId'])){
    //   $fooId=Db::name('keyword')->where('id','in',$re['fooId'])->field('keyName')->select();
    // }else{
    //   $fooId=[];
    // }
    // if(!empty($re['footprintId'])){
    //   $footprintId=Db::name('keyword')->where('id','in',$re['footprintId'])->field('keyName')->select();
    // }else{
    //   $footprintId=[];
    // }
    // $result=['keyword'=>$keyword,'exerciseId'=>$exerciseId,'fooId'=>$fooId,'footprintId'=>$footprintId];
    
  }

}